package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Emp;
import org.example.pojo.EmpQueryParam;
import org.example.pojo.PageResult;
import org.example.pojo.Result;
import org.example.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;
    @GetMapping
    //@RequestParam(defaultValue = "?")设置默认值
    //@DateTimeFormat(pattern = "?")设置日期格式
    /*public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Integer gender,
                       @DateTimeFormat(pattern = "yyyy-MM--dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM--dd") LocalDate end ){
        log.info("分页查询:{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        PageResult<Emp> pageResult=empService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageResult);
    }*/
    //当请求参数过多时可以将所有的请求参数封装为一个对象(请求参数为json时通常封装为对象)
    public Result page(EmpQueryParam empQueryParam) {
        log.info("分页查询:{}", empQueryParam);
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工:{}",emp);
        empService.save(emp);
        return Result.success(emp);
    }
    //使用数组删除员工
 /*    @DeleteMapping
    public Result delete(Integer[] ids){
        log.info("批量删除:{}", Arrays.toString(ids));
        return Result.success();
    }*/
    //使用List集合删除员工(需要在集合前面加上 @RequestParam 注解。)
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("批量删除:{}",ids);
        empService.delete(ids);
        return Result.success();
    }
    //根据id查询员工信息
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询员工信息:{}",id);
        Emp emp=empService.getInfo(id);
        return Result.success(emp);
    }
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工信息:{}",emp);
        empService.update(emp);
        return Result.success();
    }
    //查询所有员工职位为班主任的信息
    @GetMapping("/list")
    public Result list(){
        log.info("查询所有员工职位为班主任的信息");
        List<Emp> empList=empService.list();
        return Result.success(empList);
    }
}
